package communication.login
{
	import com.netease.protobuf.Message;
	import com.netease.protobuf.UInt64;
	
	import communication.CertManager;
	
	import modules.GameDispatcher;
	import modules.login.LoginEvent;
	
	import protobuf.AUTH_8881GAME_METHOD_INFO;
	import protobuf.AUTH_XINYOU_METHOD_INFO;
	import protobuf.LG_PKG_LOGIN_REQ;
	import protobuf.LG_PKG_LOGIN_REQ.E_AUTH_METHOD;

	public class LoginSocketManager
	{
		private static var instance:LoginSocketManager;

		public static function init():void
		{
			logger("登陆socket模块初始化");
			if (instance == null)
			{
				instance = new LoginSocketManager();
			}
		}

		private var loginSocket:LoginSocket;

		private var certManager:CertManager = CertManager.getInstance();

		private var dispatcher:GameDispatcher = GameDispatcher.instance;
		
		public function LoginSocketManager()
		{
			addListeners();
		}

		private function addListeners():void
		{
			dispatcher.addEventListener(LoginEvent.LOGIN,onLogin);
			
			dispatcher.addEventListener(LoginEvent.LOGIN_SUCCEED,onLoginSucceed);
		}
		
		/**
		 * 初始化登陆socket
		 */		
		private function initLoginSocket():void
		{
			LoginSocket.init();
			loginSocket = LoginSocket.instance;
		}
		
		/** 处理登陆 */
		public function onLogin(event:LoginEvent):void
		{
			initLoginSocket();
			
			var userName:String = event.data.username;
			var password:String = event.data.password;
			//加密
			var authInfo:AUTH_XINYOU_METHOD_INFO = new AUTH_XINYOU_METHOD_INFO();
			authInfo.username = certManager.convertStringToByteArray(userName);
			authInfo.password = certManager.convertStringToByteArray(password);
			authInfo.publicKey = certManager.getClientRSAPublicKey();
			var authRequest:LG_PKG_LOGIN_REQ = certManager.parseAuthRequest(authInfo, E_AUTH_METHOD.eXINYOU_AUTH_METHOD);
			doLogin(authRequest);
		}
		
		/** 处理平台登陆 */
		public function onLoginCookie(event:LoginEvent):void
		{
			initLoginSocket();
			
			var userName:String = event.data.username;
			var password:String = event.data.password;
			//加密
			var authInfo:AUTH_8881GAME_METHOD_INFO = new AUTH_8881GAME_METHOD_INFO();
			authInfo.username = event.data.username;
			authInfo.bIsAdult = event.data.isadult==1;
			authInfo.sign =  certManager.convertStringToByteArray(event.data.sign);
			authInfo.time = event.data.time;
			authInfo.userid = new UInt64(int(event.data.userid),0);
			
			authInfo.publicKey = certManager.getClientRSAPublicKey();
			var authRequest:LG_PKG_LOGIN_REQ = certManager.parseAuthRequest(authInfo, E_AUTH_METHOD.e8881GAME_AUTH_METHOD);
			doLogin(authRequest);
		}

		/** 登陆 */
		private function doLogin(authRequest:Message):void {
			if (loginSocket == null) {
				logger("try login but server info is null.");
				return;
			}
			if (loginSocket.hasConnected()) {
				loginSocket.send(LGProto.LGID_LOGIN_REQ, authRequest);
			} else {
				loginSocket.authRequest=authRequest;
				loginSocket.connect();
			}
		}
		
		/**
		 * 处理登陆成功 
		 * @param event
		 */		
		private function onLoginSucceed(event:LoginEvent):void
		{
			logger("关闭登陆socket");
			//关闭登陆socket
			loginSocket.close();
		}
	}
}
